package com.cloudcc.mobile.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudcc.cloudframe.model.Album;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.util.photo.Constants;
import com.cloudcc.mobile.util.photo.Util;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ChoicePhotoAdapter extends BaseAdapter {
	private Context context;
	private List<Album> list;

	public ChoicePhotoAdapter(Context context, List<Album> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Album getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.choice_photo_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.choicename);
			holder.icon = (ImageView) convertView
					.findViewById(R.id.icon_choice);
			holder.bg = (LinearLayout) convertView.findViewById(R.id.bg);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		String name = "";
		if (list.get(position).getmName().length() > 8) {
			name = list.get(position).getmName().substring(0, 7);
			holder.name.setText(name + list.get(position).getmNum());
		} else {
			holder.name.setText(list.get(position).getmName()
					+ list.get(position).getmNum());

		}
		if (AppContext.choiceposition == position) {
			holder.bg.setBackgroundColor(context.getResources().getColor(
					R.color.ban_touming_progress));
		} else {
			holder.bg.setBackgroundColor(context.getResources().getColor(
					R.color.quan_touming));
		}
		ImageLoader.getInstance().displayImage(
				"file://" + list.get(position).mCoverUrl, holder.icon,
				Constants.image_display_options,
				new Util.AnimateFirstDisplayListener());
		return convertView;
	}

	class ViewHolder {
		TextView name;
		ImageView icon;
		LinearLayout bg;
	}
}
